hash.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. from __future__ import absolute_import
  2. import hashlib
  3. import logging
  4. import sys
  5. from pip._internal.cli.base_command import Command
  6. from pip._internal.cli.status_codes import ERROR, SUCCESS
  7. from pip._internal.utils.hashes import FAVORITE_HASH, STRONG_HASHES
  8. from pip._internal.utils.misc import read_chunks, write_output
  9. from pip._internal.utils.typing import MYPY_CHECK_RUNNING
  10. if MYPY_CHECK_RUNNING:
  11. from optparse import Values
  12. from typing import List
  13. logger = logging.getLogger(__name__)
  14. class HashCommand(Command):
  15. """
  16. Compute a hash of a local package archive.
  17. These can be used with --hash in a requirements file to do repeatable
  18. installs.
  19. """
  20. usage = '%prog [options] <file> ...'
  21. ignore_require_venv = True
  22. def add_options(self):
  23. # type: () -> None
  24. self.cmd_opts.add_option(
  25. '-a', '--algorithm',
  26. dest='algorithm',
  27. choices=STRONG_HASHES,
  28. action='store',
  29. default=FAVORITE_HASH,
  30. help='The hash algorithm to use: one of {}'.format(
  31. ', '.join(STRONG_HASHES)))
  32. self.parser.insert_option_group(0, self.cmd_opts)
  33. def run(self, options, args):
  34. # type: (Values, List[str]) -> int
  35. if not args:
  36. self.parser.print_usage(sys.stderr)
  37. return ERROR
  38. algorithm = options.algorithm
  39. for path in args:
  40. write_output('%s:\n--hash=%s:%s',
  41. path, algorithm, _hash_of_file(path, algorithm))
  42. return SUCCESS
  43. def _hash_of_file(path, algorithm):
  44. # type: (str, str) -> str
  45. """Return the hash digest of a file."""
  46. with open(path, 'rb') as archive:
  47. hash = hashlib.new(algorithm)
  48. for chunk in read_chunks(archive):
  49. hash.update(chunk)
  50. return hash.hexdigest()